import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../resources.dart';

/// created by hcl
/// on 2024/4/13
/// DialogTextField
/// desc
typedef ValueGetter<T> = T Function(String result);

class DialogTextField {
  static show(
      {required String title,
      required String des,
      required ValueChanged<String> onResult,
      required ValueGetter<String> checkResult,
      String hint = "",
      String cancelText = "",
      String confirmText = "",
      TextInputType keyboardType = TextInputType.text,
      bool showPassword = false,
      bool backBySelf = true,
      bool dismissible = false}) {
    Toast.showDialogWidget(
        child: _DialogTextField(
            title: title,
            des: des,
            onResult: onResult,
            checkResult: checkResult,
            hint: hint,
            cancelText: cancelText,
            confirmText: confirmText,
            keyboardType: keyboardType,
            backBySelf: backBySelf,
            showPassword: showPassword),
        dismissible: dismissible);
  }
}

class _DialogTextField extends StatefulWidget {
  ValueChanged<String> onResult;
  ValueGetter<String> checkResult;
  String title;
  String des;
  String hint;
  String cancelText;
  String confirmText;
  TextInputType keyboardType;
  bool showPassword;
  bool backBySelf;

  _DialogTextField(
      {required this.title,
      required this.des,
      required this.onResult,
      required this.checkResult,
      this.hint = '',
      this.cancelText = '',
      this.confirmText = '',
      this.keyboardType = TextInputType.text,
      this.backBySelf = false,
      this.showPassword = false});

  @override
  State<StatefulWidget> createState() => _DialogTextFieldState();
}

class _DialogTextFieldState extends State<_DialogTextField> {
  FocusNode focusNode = FocusNode();
  TextEditingController editingController = TextEditingController();
  String? error;

  @override
  Widget build(BuildContext context) {
    List<TextInputFormatter> tempList = [];
    if (widget.keyboardType == TextInputType.number ||
        widget.keyboardType == TextInputType.phone) {
      tempList.add(FilteringTextInputFormatter.digitsOnly);
    }
    return SizedBox(
      width: designWidth.ss,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 30.ss),
        padding: EdgeInsets.symmetric(horizontal: 16.ss),
        decoration: BoxDecoration(
          color: ColorUtils.color_FFFFFF,
          borderRadius: BorderRadius.all(Radius.circular(8.ss)),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: 16.ss),
            Text(widget.title, style: medium018),
            SizedBox(height: 16.ss),
            Container(
              height: 48.ss,
              alignment: Alignment.topCenter,
              child: TextField(
                maxLines: 1,
                focusNode: focusNode,
                controller: editingController,
                keyboardType: widget.keyboardType,
                style: normal318,
                obscureText: widget.showPassword,
                inputFormatters: tempList,
                decoration: InputDecoration(
                  counterText: '',
                  // errorText: error,
                  hintText: widget.hint,
                  hintStyle: normalC18,
                  contentPadding: EdgeInsets.symmetric(horizontal: 10.ss),
                  filled: true,
                  fillColor: ColorUtils.color_FFFFFF,
                  border: _getBorder(),
                  enabledBorder: _getBorder(),
                  disabledBorder: _getBorder(),
                  focusedBorder: _getBorder(),
                ),
                onChanged: (value) {
                  setState(() {
                    error = null;
                  });
                },
              ),
            ),
            Container(
              height: 16.ss,
              alignment: Alignment.centerLeft,
              child: Text(error ?? '', style: mediumAD12),
            ),
            Row(
              children: [
                widget.cancelText.isNotEmpty
                    ? Expanded(
                        child: Container(
                          child: commonButton(() {
                            Nav.pop();
                          }, widget.cancelText, isConfirm: false),
                        ),
                      )
                    : const SizedBox(),
                Expanded(
                  child: Container(
                    child: commonButton(() {
                      String check = widget.checkResult(editingController.text);
                      if (check.isNotEmpty) {
                        setState(() {
                          error = check;
                        });
                      } else {
                        if (widget.backBySelf) {
                          Nav.pop(result: backResult(NORMAL_CODE, ''));
                        }
                        widget.onResult(editingController.text);
                      }
                    },
                        widget.confirmText.isNotEmpty
                            ? widget.confirmText
                            : word.confirm),
                  ),
                )
              ],
            ),
            // SizedBox(height: widget.des.isNotEmpty ? 8.ss : 0),
            // Text(widget.des, style: normal712),
            SizedBox(height: 8.ss),
          ],
        ),
      ),
    );
  }

  OutlineInputBorder _getBorder() {
    return OutlineInputBorder(
        borderSide: const BorderSide(color: ColorUtils.color_CCCCCC),
        borderRadius: BorderRadius.circular(8.ss));
  }
}
